﻿using System;
using System.Globalization;
using System.Collections.Generic;
using System.Text;

using log4net;
using log4net.Core;

namespace EAI.Logging
{
    public class EAILogImpl : LogImpl, IEAILog
    {/// <summary>
        /// The fully qualified name of this declaring type not the type of any subclass.
        /// </summary>
        public readonly static Type ThisDeclaringType = typeof(EAILogImpl);

        public EAILogImpl(ILogger logger)
            : base(logger)
        {
        }

        public virtual void LevelLog(EAILogLevel level, object message)
        {
            Logger.Log(ThisDeclaringType, ConvertLogLevel(level), message, null);
        }

        public virtual void LevelLog(EAILogLevel level, object message, Exception exception)
        {
            Logger.Log(ThisDeclaringType, ConvertLogLevel(level), message, exception);
        }

        public virtual void LevelLogFormat(EAILogLevel level, string format, params object[] args)
        {
            Logger.Log(ThisDeclaringType, ConvertLogLevel(level), String.Format(CultureInfo.InvariantCulture, format, args), null);
        }

        public virtual void LevelLogFormat(EAILogLevel level, IFormatProvider provider, string format, params object[] args)
        {
            Logger.Log(ThisDeclaringType, ConvertLogLevel(level), String.Format(provider, format, args), null);
        }

        private Level ConvertLogLevel(EAILogLevel level)
        {
            Level retval = Level.Off;
            switch (level)
            {
                case EAILogLevel.Debug:
                    retval = Level.Debug;
                    break;
                case EAILogLevel.Info:
                    retval = Level.Info;
                    break;
                case EAILogLevel.Error:
                    retval = Level.Error;
                    break;
                case EAILogLevel.Fatal:
                    retval = Level.Fatal;
                    break;
                case EAILogLevel.Warn:
                    retval = Level.Warn;
                    break;
                case EAILogLevel.Verbose:
                    retval = Level.Verbose;
                    break;
                default:
                    retval = Level.Off;
                    break;
            }

            return retval;
        }
    }
}
